package com.block.merchant.service.infotype;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.service.LogFactory;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantDataPoolInfo;
import com.block.db.mapper.MerchantDataPoolInfoMapper;
import com.block.merchant.model.dto.MerchantOrderDTO;
import com.block.merchant.model.result.DataPushResult;
import com.block.merchant.model.result.MatchMerchantInfo;
import com.block.merchant.service.IMerchantDataAllocateInfoService;
import com.block.merchant.service.IMerchantHandleService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;

@Service
public class MerchantCRMHandleService implements IMerchantHandleService {

    private static final Logger log = LogFactory.getMerchantLog();
    private static final Logger sourceLog = LogFactory.getSourceLog();

    @Resource
    MerchantDataPoolInfoMapper merchantDataPoolInfoMapper;
    @Autowired
    IMerchantDataAllocateInfoService merchantDataAllocateInfoService;

    /**
     *
     * @param merchantBaseInfo
     * @param memberSourceInfo
     * @return
     */
    @Override
    public MatchMerchantInfo matchWithMerchant(MerchantBaseInfo merchantBaseInfo, MerchantOrderDTO merchantOrderDTO, MemberSourceInfo memberSourceInfo) {

        MatchMerchantInfo matchMerchantInfo =  new MatchMerchantInfo();

        MerchantDataPoolInfo dataPoolInfo = merchantDataPoolInfoMapper.selectEntityByInfoIdAndUid(merchantBaseInfo.getId(), memberSourceInfo.getUid());
        if (ObjectUtil.isEmpty(dataPoolInfo)){
            matchMerchantInfo.setMatchSuccess(true);
        }else{
            matchMerchantInfo.setMatchSuccess(false);
        }

        matchMerchantInfo.setShowPosition(merchantBaseInfo.getShowPosition());
        matchMerchantInfo.setInfoId(merchantBaseInfo.getId());
        matchMerchantInfo.setInfoType(merchantBaseInfo.getInfoType());
        matchMerchantInfo.setTaskId(merchantOrderDTO.getTaskId());
        matchMerchantInfo.setProfitAmount(merchantOrderDTO.getPriceAmount());

        // 用户的协议地址
        matchMerchantInfo.setProtocolUrl(merchantBaseInfo.getApiProtocolUrl());
        matchMerchantInfo.setApproveFlag(merchantBaseInfo.getApproveFlag());
        log.info("【商户CRM匹配结果】【infoId】{}【taskId】{}【uid】{}【matchMerchantInfo】{}",
                merchantBaseInfo.getId(), merchantOrderDTO.getTaskId(), memberSourceInfo.getUid(), JSON.toJSONString(matchMerchantInfo));
        return matchMerchantInfo;
    }


    @Override
    public DataPushResult pushToMerchant(MerchantOrderDTO merchantOrderDTO, MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {

        DataPushResult dataPushResult = new DataPushResult();

        MerchantDataPoolInfo dataPoolInfo = merchantDataPoolInfoMapper.selectEntityByInfoIdAndUid(merchantOrderDTO.getInfoId(), memberSourceInfo.getUid());
        if (ObjectUtil.isNotEmpty(dataPoolInfo)){
            dataPushResult.setPushSuccess(false);
            dataPushResult.setMessage("该用户已经推送过该商户");
            return dataPushResult;
        }

        dataPoolInfo = new MerchantDataPoolInfo();
        dataPoolInfo.setDateInfo(LocalDate.now().toString());
        dataPoolInfo.setInfoId(merchantOrderDTO.getInfoId());
        dataPoolInfo.setTaskId(merchantOrderDTO.getTaskId());
        dataPoolInfo.setDeptId(merchantOrderDTO.getDeptId());
        dataPoolInfo.setUid(memberSourceInfo.getUid());
        dataPoolInfo.setOrderId(merchantOrderDTO.getOrderId());
        dataPoolInfo.setOrderItemId(merchantOrderDTO.getOrderItemId());
        dataPoolInfo.setPriceAmount(merchantOrderDTO.getPriceAmount());
        dataPoolInfo.setCostAmount(merchantOrderDTO.getCostAmount());
        dataPoolInfo.setPayAmount(merchantOrderDTO.getPayAmount());
        dataPoolInfo.setAppCode(memberSourceInfo.getAppCode());
        dataPoolInfo.setAppName(memberSourceInfo.getAppName());
        dataPoolInfo.setChannelCode(memberSourceInfo.getChannelCode());
        dataPoolInfo.setChannelName(memberSourceInfo.getChannelName());
        dataPoolInfo.setStatus(0);
        dataPoolInfo.setStarNumber(0);
        dataPoolInfo.setStateInfo(0);

        boolean save = merchantDataPoolInfoMapper.insert(dataPoolInfo)>0;
        if (save){
            dataPushResult.setPushSuccess(true);
            dataPushResult.setSettleFlag(true);
        }else{
            dataPushResult.setPushSuccess(false);
            dataPushResult.setSettleFlag(false);
        }
        dataPushResult.setPushOrderId(String.valueOf(dataPoolInfo.getId()));

        log.info("【商户CRM推送结果】【infoId】{}【uid】{}【matchMerchantInfo】{}",
                merchantOrderDTO.getInfoId(),  memberSourceInfo.getUid(), JSON.toJSONString(dataPushResult));

        // 异步处理 商户数据的自动分配
        merchantDataAllocateInfoService.autoAllocateToUser(String.valueOf(dataPoolInfo.getId()));

        return dataPushResult;
    }

    @Override
    public MatchMerchantInfo sourceCheckWithMerchant(MerchantBaseInfo merchantBaseInfo, MerchantOrderDTO merchantOrderDTO, MemberSourceInfo memberSourceInfo) throws Exception {

        MatchMerchantInfo matchMerchantInfo =  new MatchMerchantInfo();

        MerchantDataPoolInfo dataPoolInfo = merchantDataPoolInfoMapper.selectEntityByInfoIdAndUid(merchantBaseInfo.getId(), memberSourceInfo.getUid());
        if (ObjectUtil.isEmpty(dataPoolInfo)){
            matchMerchantInfo.setMatchSuccess(true);
        }else{
            matchMerchantInfo.setMatchSuccess(false);
        }

        matchMerchantInfo.setShowPosition(merchantBaseInfo.getShowPosition());
        matchMerchantInfo.setInfoId(merchantBaseInfo.getId());
        matchMerchantInfo.setInfoType(merchantBaseInfo.getInfoType());
        matchMerchantInfo.setTaskId(merchantOrderDTO.getTaskId());
        matchMerchantInfo.setProfitAmount(merchantOrderDTO.getPriceAmount());

        // 用户的协议地址
        matchMerchantInfo.setProtocolUrl(merchantBaseInfo.getApiProtocolUrl());
        matchMerchantInfo.setApproveFlag(merchantBaseInfo.getApproveFlag());
        sourceLog.info("【商户CRM匹配结果】【infoId】{}【taskId】{}【uid】{}【matchMerchantInfo】{}",
                merchantBaseInfo.getId(), merchantOrderDTO.getTaskId(), memberSourceInfo.getUid(), JSON.toJSONString(matchMerchantInfo));
        return matchMerchantInfo;
    }
}
